<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Sms_lib{

	private $user_id ;
	function Sms_lib()
	{
		$this->CI = &get_instance();
		$this->CI->load->model('smsmodel');
		$this->CI->load->library('form_validation');
	}


	function send()
	{
		$data = array('list'=>'s');
		$this->CI->form_validation->set_rules('mobile_no', 'Mobile No', 'trim|required|xss_clean');
		$this->CI->form_validation->set_value('mobile_no');
		$data['errors'] = array();
		if($this->CI->form_validation->run())
		{
			$insert = array('user_id'=>$this->CI->tank_auth->get_user_id(),
							'message'=> $this->CI->input->post('message'));
			$message_id = $this->CI->smsmodel->insert($insert);
			$this->CI->load->model('tomobilemodel');
			
			$this->CI->tomobilemodel->insert(array('sms_id'=>$message_id,'mobile_no'=>$this->CI->input->post('mobile_no')));
			$this->CI->session->set_flashdata('flashMessage','Message sent Successfully');
			redirect('/sms/send');
		}
		else
		{
			$errors = $this->CI->tank_auth->get_error_message();
				foreach ($errors as $k => $v)	$data['errors'][$k] = $this->CI->lang->line($v);
		}
		return $this->CI->load->view('sms/send', $data, TRUE);
		die();
	}

	function sentlist()
	{
		$result = $this->CI->smsmodel->getSmsListById($this->user_id);
		$this->CI->form_validation->set_rules('mobile_no', 'Mobile No', 'trim|required|xss_clean');
		$data = array('list'=>$result);
		$this->CI->form_validation->set_value('mobile_no');
		$data['errors'] = array();
		if($this->CI->form_validation->run())
		{
		}
		else
		{
			$errors = $this->CI->tank_auth->get_error_message();
			print_r($errors);
			if (isset($errors['banned'])) {								// banned user
				$this->_show_message($this->CI->lang->line('auth_message_banned').' '.$errors['banned']);

			} elseif (isset($errors['not_activated'])) {				// not activated user
				//redirect('/auth/send_again/');

			} else {													// fail
				foreach ($errors as $k => $v)	$data['errors'][$k] = $this->CI->lang->line($v);
			}
		}
		return $this->CI->load->view('sms/send', $data, TRUE);
	}

}
